/**
 * Created by limeng on 16/7/19.
 */
delegateApp.controller('UpdateProposalCtrl', function ($scope, $rootScope, $http, $routeParams) {
    $scope.proposal = {};
    $scope.proposalId = $routeParams.proposalId;
    $scope.isUpdate = true;
    $scope.title = '修改提案';

    $http.get('/proposal/'
        + $scope.proposalId
    ).success(
        function (data) {
            if (data.state == true) {
                $scope.proposal = data.proposal;
                if ($scope.proposal.id && $scope.proposal.proposalState == 'DRAFT') {
                    $scope.isDraft = true;
                }
                var contentElem = document.getElementById("textEditor");
                contentElem.innerHTML = $scope.proposal.content;
            } else {
                showAlert('查询失败', '原因是:'+data.result);
            }
        }
    ).error(
        function () {
            showAlert('查询失败', '请联系开发者进行维护');
        }
    );

    $scope.publishDraft = function () {
        if ($scope.proposal.title == '') {
            bootbox.alert("请输入标题!");
            return;
        }
        if (!$scope.proposal.type || $scope.proposal.type == '') {
            bootbox.alert("请选择提案类型!");
            return;
        }
        $scope.postProposal = {};

        $scope.postProposal.id = $scope.proposalId;
        $scope.postProposal.title = $scope.proposal.title;
        $scope.postProposal.content = $('#textEditor')[0].innerHTML;
        $scope.postProposal.state = 1;
        $scope.postProposal.attachments = $scope.proposal.attachments;
        $scope.postProposal.type = $scope.proposal.type;

        $http.post('/proposal/update', $scope.postProposal)
            .success(function (data) {
                if (data.state == true) {
                    bootbox.alert('发布成功, 提案已成功发布，请耐心等待主席团审核', function () {
                        history.go(-1);
                    });
                } else {
                    bootbox.alert('发布失败,提案被服务器拒绝，错误提示为：\n' + data.result);
                }
            }).error(function () {
            showAlert('发布失败', '服务器没有响应请求！')
        });
    };

    $scope.publishProposal = function () {
        if ($scope.proposal.title == '') {
            bootbox.alert("请输入标题!");
            return;
        }
        if (!$scope.proposal.type || $scope.proposal.type == '') {
            bootbox.alert("请选择提案类型!");
            return;
        }
        $scope.postProposal = {};

        $scope.postProposal.id = $scope.proposalId;
        $scope.postProposal.title = $scope.proposal.title;
        $scope.postProposal.content = $('#textEditor')[0].innerHTML;
        $scope.postProposal.attachments = $scope.proposal.attachments;
        $scope.postProposal.type = $scope.proposal.type;

        $http.post('/proposal/update', $scope.postProposal)
            .success(function (data) {
                if (data.state == true) {
                    bootbox.alert('修改成功!', function () {
                        history.go(-1);
                    });
                } else {
                    bootbox.alert('修改失败,提案被服务器拒绝，错误提示为：\n' + data.result);
                }
            }).error(function () {
            showAlert('修改失败', '服务器没有响应请求！')
        });
    };

    $scope.postAttachment = function ($flow) {
        var files = $flow.files;
        if (files.length == 0) {
            showAlert('上传附件', '没有任何附件被上传');
            return;
        }
        var fileArr = [];
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            if (file.progress() == 1 && !file.error) {
                var fileElement = {
                    filename : file.name,
                    size : file.size / 1024.0,
                    realName : file.uniqueIdentifier
                };
                fileArr.push(fileElement);
            }
        }
        $scope.proposal.attachments = fileArr;
    };
});